package com.sevntu.apvs.dModel.tests;

import junit.framework.Assert;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.junit.BeforeClass;
import org.junit.Test;

import com.sevntu.apvs.dModel.CircuitLoader;
import com.sevntu.apvs.dModel.CircuitTestLoader;
import com.sevntu.apvs.dModel.DModel;
import com.sevntu.apvs.dModel.DModelException;

/**
 * Test class for checking the loading tests for the input scheme.
 * 
 * @author <a href="mailto:rd.ryly@gmail.com">Ruslan Diachenko</a>
 */
public class CircuitTestLoaderTest {

	/** Use for log message generation */
	private static final Logger LOG = Logger.getLogger(CircuitTestLoaderTest.class.getName());

	private CircuitTestLoader testLoader;

	private int[][] tests;

	private int circuitEntryNum;

	@BeforeClass
	public static void initLog4jProperties() {
		PropertyConfigurator.configure("config/log4j.properties");
	}

	@Test
	public void testGetCircuitTest() throws DModelException {
		testLoader = new CircuitTestLoader("src/com/sevntu/apvs/dModel/testinputs/test.txt");
		circuitEntryNum = 4;
		tests = testLoader.getCircuitTest(circuitEntryNum);

		Assert.assertEquals(0, tests[0][0]);
		Assert.assertEquals(1, tests[0][1]);
		Assert.assertEquals(2, tests[0][2]);
		Assert.assertEquals(1, tests[0][3]);

		Assert.assertEquals(1, tests[1][0]);
		Assert.assertEquals(0, tests[1][1]);
		Assert.assertEquals(1, tests[1][2]);
		Assert.assertEquals(1, tests[1][3]);

		LOG.info("Test was successfuly complited.");
	}

	@Test
	public void testGetTestFromXML() throws DModelException {
		CircuitLoader xmlLoader;
		DModel dModel;

		// XML scheme loading
		xmlLoader = new CircuitLoader();
		dModel = xmlLoader.getDModel("src/com/sevntu/apvs/dModel/testinputs/circuit.xml");

		Assert.assertEquals("4OR-NOT", dModel.getElemList().get(0).name);
		Assert.assertEquals(1, dModel.getElemList().get(0).id);

		Assert.assertEquals(1, dModel.getCircuitList().get(0).getCircuitId());
		Assert.assertEquals(0, dModel.getCircuitList().get(0).getElemNumOut());
		Assert.assertEquals(1, dModel.getCircuitList().get(0).getElemContactOut());
		Assert.assertEquals(1, dModel.getCircuitList().get(0).getElemNumIn());
		Assert.assertEquals(1, dModel.getCircuitList().get(0).getElemContactIn());

		Assert.assertEquals(1, xmlLoader.getSchemeEntryNum());

		// Scheme test loading
		testLoader = new CircuitTestLoader("src/com/sevntu/apvs/dModel/testinputs/test2.txt");
		circuitEntryNum = xmlLoader.getSchemeEntryNum();
		tests = testLoader.getCircuitTest(circuitEntryNum);

		Assert.assertEquals(0, tests[0][0]);
		Assert.assertEquals(2, tests[1][0]);
		Assert.assertEquals(1, tests[2][0]);
		Assert.assertEquals(2, tests[3][0]);

		LOG.info("Test was successfuly complited.");
	}
}
